import 'package:flutter/material.dart';
import 'package:my_app/conponate/user_info.dart';
import '../tools/http_server.dart';

class SearchingContent extends StatefulWidget {
  String query = "";
  SearchingContent(this.query);

  @override
  _SearchingContentState createState() => _SearchingContentState(query);
}

class _SearchingContentState extends State<SearchingContent> {

  String query = "";

  _SearchingContentState(String query){
    this.query = query;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: HttpServer.getData(query),
      builder: (BuildContext context, AsyncSnapshot<List> snapshot){
        if(snapshot.connectionState == ConnectionState.done){
          if(snapshot.hasError){
            return Text("error");
          }else{
            var data = snapshot.data;
            print("data: $data");
            List<Map<String, dynamic>> tiles = (data as List).cast();
            print("tiles: ${tiles.length}");
            return ListView.builder(
                itemCount: tiles.length,
                shrinkWrap: true,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title:  Text('${(tiles)[index]["name"]}'),
                    subtitle: Text('${(tiles)[index]["type"]}'),
                    trailing: Icon(Icons.star),
                    onTap: (){
                      showDialog<Null>(
                        context: context,
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          bool isStar = (tiles)[index]["star"] == "yes" ? true : false;
                          print(isStar);
                          if(isStar){
                            // todo: 判断用户是否收藏
                            HttpServer.star(UserInfo.userName, (tiles)[index]["gname"], "取消收藏");
                            return new AlertDialog(
                              title: new Text('已取消收藏'),
                              actions: <Widget>[
                                new FlatButton(
                                  child: new Text('确定'),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                ),
                              ],
                            );
                          } else {
                            HttpServer.star(UserInfo.userName, (tiles)[index]["name"], "收藏");
                            return new AlertDialog(
                              title: new Text('已收藏'),
                              actions: <Widget>[
                                new FlatButton(
                                  child: new Text('确定'),
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                ),
                              ],
                            );
                          }
                        },
                      ).then((val) {
                        print(val);
                      });
                    },
                  );
                }
            );
          }
        } else{
          return Align(
            child: CircularProgressIndicator(),
            alignment: Alignment.center,
          );
        }
      },
    );
  }
}



